package cn.yunhe.controller;

import cn.yunhe.entity.Class;
import cn.yunhe.entity.*;
import cn.yunhe.services.IPaperServices;
import cn.yunhe.services.IScoreServices;
import cn.yunhe.services.IStudentServices;
import cn.yunhe.util.ListToXml;
import com.github.pagehelper.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/22.
 */
@RestController
@RequestMapping("/stu")
public class StudentController {

    @Resource
    private IStudentServices studentServices;

    @Resource
    private IScoreServices scoreServices;

    @Resource(name = "iPaperServices")
    private IPaperServices paperServices;

    @RequestMapping("/getlist")
    public ModelAndView test(){
        List<Student> list = studentServices.getLikeStudent();
        for (Student student:list){
            System.out.println(student.toString());
        }
        ModelAndView mv = new ModelAndView("ftl/test");
        return mv;
    }

    /*跳转exam视图页面*/
    @RequestMapping("/toExam/{id}")
    public ModelAndView toExam(@PathVariable int id){
        ModelAndView mv = new ModelAndView("student/exam");
        mv.addObject("id",id);
        return mv;
    }

    //加载试卷所有试题
    @RequestMapping("/loadPaper")
    public Map<String,Object> loadPaper(int id){
        ListToXml lt = new ListToXml();
        //获取试卷
        Paper paper = paperServices.showOnePaper(id);
        //获取当前试卷试题集合
        List<StudentProblem> list = lt.xmlToList(paper.getContent());
        //筛选试题类型 并分类
        //单选集合
        List<StudentProblem> onlyList = new ArrayList<>();
        //多选集合
        List<StudentProblem> manyList = new ArrayList<>();
        //单选集合
        List<StudentProblem> judgeList = new ArrayList<>();
        for (StudentProblem p : list){
            //单选题判断
            if(p.getType().equals("单选题")){
                onlyList.add(p);
            }
            //多选题判断
            if(p.getType().equals("多选题")){
                manyList.add(p);
            }
            //判断题判断
            if(p.getType().equals("判断题")){
                judgeList.add(p);
            }
        }
        //试卷类型分值
        String typeScoreStr = paper.getStruc();


        Map<String,Object> map = new HashMap<>();
        map.put("paper",paper);
        map.put("onlyList",onlyList);
        map.put("manyList",manyList);
        map.put("judgeList",judgeList);
        System.out.println("onlyList====="+onlyList.size());
        System.out.println("manyList====="+manyList.size());
        System.out.println("judgeList====="+judgeList.size());
        return map;
    }

    @RequestMapping(value = "/getScore")
    public Map getScore(@RequestBody WebPaper webPaper){

        //学生id
        int sid = webPaper.getSid();
        //试卷id
        int pid = webPaper.getPid();

        //单选试题
        List<StudentProblem> onlyList = webPaper.getProblems().get("onlyList");
        //多选试题
        List<StudentProblem> manyList = webPaper.getProblems().get("manyList");
        //判断试题
        List<StudentProblem> judgeList = webPaper.getProblems().get("judgeList");

        //获取试卷
        Paper paper = paperServices.showOnePaper(pid);

        int onlyTypeScore=0;
        int manyTypeScore=0;
        int judgeTypeScore=0;
        int manyTypeHalfScore = 0;

        String a = ListToXml.getTypeScore("单选题",paper.getStruc());
        String b = ListToXml.getTypeScore("多选题",paper.getStruc());
        String c = ListToXml.getTypeScore("判断题",paper.getStruc());
        String d = ListToXml.getTypeScore("半对多选题",paper.getStruc());
        //单选题类型分值
        if (a != null){
            onlyTypeScore = Integer.parseInt(ListToXml.getTypeScore("单选题",paper.getStruc()));
        }
        if(b!=null){
            //多选题类型分值
            manyTypeScore = Integer.parseInt(ListToXml.getTypeScore("多选题",paper.getStruc()));
        }
        if(c!=null){
            //判断题类型分值
            judgeTypeScore = Integer.parseInt(ListToXml.getTypeScore("判断题",paper.getStruc()));
        }
        if(d != null){
            manyTypeHalfScore = (int) Math.floor(manyTypeScore/2);
        }

        //正确的单选题个数
        int onlyTrue = 0;
        for (StudentProblem sp :onlyList){
            if (sp.getCorrectAnswer().equals(sp.getStudentAnswer()[0])){
                onlyTrue++;
            }
        }
        //正确的判断题题个数
        int judgeTrue = 0;
        for (StudentProblem sp :judgeList){
            if (sp.getCorrectAnswer().equals(sp.getStudentAnswer()[0])){
               judgeTrue++;
            }
        }
        //正确的多选题个数
        int manyTrue = 0;
        int manyHalfTrue = 0;
        for (StudentProblem sp :manyList){
            String[] trues = sp.getCorrectAnswer().split(",");
            String[] stus = ListToXml.removeArray(sp.getStudentAnswer());
            //正确个数
            int correctCount = 0;

            for (int i = 0;i<trues.length;i++){
                for (int j = 0;j<stus.length;j++){
                    if (trues[i].equals(stus[j])){
                        correctCount++;
                    }
                }
            }
            //如果正确个数与正确答案的个数相等 且与自己的答案个数相等 是全对
            if(correctCount == trues.length &&  correctCount == stus.length){
                manyTrue++;
            }
            //如果正确个数小于正确答案的个数 且与自己的答案个数相等 是半对
            if (correctCount == stus.length && correctCount < trues.length && stus.length != 0){
                manyHalfTrue++;
            }
        }

        //成绩
        int score = onlyTypeScore*onlyTrue+judgeTypeScore*judgeTrue+manyTypeScore*manyTrue+manyTypeHalfScore*manyHalfTrue;

        //保存试卷内容
        StringBuffer contentSB = new StringBuffer();
        StringBuffer onlySB ;
        if (onlyList.size() != 0){
            onlySB = new StringBuffer(ListToXml.listToXml(onlyList));
            onlySB.delete(0,10);
            onlySB.delete(onlySB.length()-11,onlySB.length());
            contentSB.append(onlySB.toString());
        }
        StringBuffer manySB;
        if (manyList.size() != 0){
            manySB = new StringBuffer(ListToXml.listToXml(manyList));
            manySB.delete(0,10);
            manySB.delete(manySB.length()-11,manySB.length());
            contentSB.append(manySB.toString());
        }

        StringBuffer judgeSB ;
        if (judgeList.size() != 0){
            judgeSB = new StringBuffer(ListToXml.listToXml(judgeList));
            judgeSB.delete(0,10);
            judgeSB.delete(judgeSB.length()-11,judgeSB.length());
            contentSB.append(judgeSB.toString());
        }

        //学生试卷内容
        String content = "<problems>"+contentSB.toString()+"</problems>";
        // 试卷满分成绩
        int full = paper.getFull();

        Map<String,Object> scoreMap = new HashMap<>();
        scoreMap.put("sid",sid);
        scoreMap.put("pid",pid);
        scoreMap.put("content",content);
        scoreMap.put("score",score);
        scoreMap.put("full",full);

        int succcess = scoreServices.addStuScore(scoreMap);
        System.out.println(succcess);

        //返回数据
        Map<String,Object> m = new HashMap<>();
        m.put("score",score);
        return m;
    }

    //获取需要参见的考试
    @RequestMapping("/getZxPaper")
    public List<Paper> getPaperByClass(HttpSession session){
        //获取session域中的学生对象
        Student student= new Student();
        Class c = new Class();
        c.setClass_id(3);
        student.setaClass(c);
        session.setAttribute("stu",student);

        Student s = (Student) session.getAttribute("stu");
        int cid = s.getaClass().getClass_id();

        List<Paper> list = paperServices.getPaperByClassId(cid);
        return list;
    }

    //获取模拟试卷中的条件的数据
    @RequestMapping("/getCondition")
    public Map<String,Object> getMnCondition(HttpSession session){

        Student s = (Student) session.getAttribute("stu");
        int cid = s.getaClass().getClass_id();

        Map<String,Object> map = studentServices.getMnCondition(cid);

        return map;
    }
    //获取模拟考试多条件查询后数据
    @RequestMapping("/paperPage")
    public Map getSimPapers(int curPageNum, int pageSize,int course_id,int ptype_id,String year,int latest,String names,HttpSession session){
        System.out.println(course_id+"==="+ptype_id+"==="+year+"==="+latest+"==="+names);

        Student s = (Student) session.getAttribute("stu");
        int cid = s.getaClass().getClass_id();

        Map<String,Object> map = new HashMap<>();
        map.put("curPageNum",curPageNum);
        map.put("pageSize",pageSize);
        map.put("cid",cid);
        map.put("course_id",course_id);
        map.put("ptype_id",ptype_id);
        map.put("year",year);
        map.put("latest",latest);
        map.put("names",names);
        Page page = studentServices.getMcPapers(map);
        map.put("page",page);
        map.put("totalPages",page.getPages());
        return map;
    }
    //获取当前学生的成绩
    @RequestMapping("/getCurStuScorePage")
    public Map getCurStuScore(int curPageNum, int pageSize){
        //学生id
        int sid = 1;
        Map<String,Object> map = new HashMap<>();
        map.put("curPageNum",curPageNum);
        map.put("pageSize",pageSize);
        map.put("sid",sid);

        Page page = studentServices.getCurrStuScores(map);
        map.put("page",page);
        map.put("totalPages",page.getPages());
        return map;
    }

    /*
    * 跳转Student主页面*/
    @RequestMapping("/toIndex")
    public ModelAndView toStuIndex(){
        ModelAndView mv = new ModelAndView("student/index");
        return mv;
    }
    /*
    * 跳转Student头部*/
    @RequestMapping("/toTop")
    public ModelAndView toStuTop(){
        ModelAndView mv = new ModelAndView("student/top");
        return mv;
    }
    /*
   * 跳转Student考试板块*/
    @RequestMapping("/toAllExam")
    public ModelAndView toExamAll(){
        ModelAndView mv = new ModelAndView("student/exam_all");
        return mv;
    }
    /*
    * 跳转Student底部*/
    @RequestMapping("/toBot")
    public ModelAndView toStuBot(){
        ModelAndView mv = new ModelAndView("student/bottom");
        return mv;
    }
    /*
    * 跳转Student内容板块*/
    @RequestMapping("/toHome")
    public ModelAndView toStuHome(){
        ModelAndView mv = new ModelAndView("student/home");
        return mv;
    }



    public IStudentServices getStudentServices() {
        return studentServices;
    }

    public void setStudentServices(IStudentServices studentServices) {
        this.studentServices = studentServices;
    }

    public IPaperServices getPaperServices() {
        return paperServices;
    }

    public void setPaperServices(IPaperServices paperServices) {
        this.paperServices = paperServices;
    }

    public IScoreServices getScoreServices() {
        return scoreServices;
    }

    public void setScoreServices(IScoreServices scoreServices) {
        this.scoreServices = scoreServices;
    }
}
